package com.easyj.base.constant;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * Description:配置文件配置项
 */
@Configuration
public class ConstantProperties implements InitializingBean {
     /**
      * 本地保存路径
      */
	 @Value("${upload.local-path}")
     private String localPath;
	 
	 @Value("${upload.local-url:}") //default=''
	 private String localUrl;
	/**
     * 阿里不同的服务器，地址不同
     */
    @Value("${alioss.file.endpoint}")
    private String alioss_file_endpoint;

    /**
     * OSS控制台获取
     */
    @Value("${alioss.file.keyid}")
    private String alioss_file_keyid;

    /**
     * OSS控制台获取
     */
    @Value("${alioss.file.keysecret}")
    private String alioss_file_keysecret;

    /**
     * 文件上传路径
     */
    @Value("${alioss.file.path}")
    private String alioss_file_path;

    /**
     * 这个自己创建bucket时的命名，控制台创建也行，代码创建也行
     */
    @Value("${alioss.file.bucket}")
    private String alioss_file_bucket;

    public static String LOCAL_PATH;
    public static String LOCAL_URL;
    public static String ALIOSS_END_POINT;
    public static String ALIOSS_ACCESS_KEY_ID;
    public static String ALIOSS_ACCESS_KEY_SECRET;
    public static String ALIOSS_BUCKET_NAME;
    public static String ALIOSS_FILE_PATH;


    private String handlePath(String path) {
		 path = StringUtils.replace(path, "\\", "/");
		 if(!path.endsWith("/")) {
			 path=path+"/";
		 }
		 return path;
	}
    
    private String handleLocalUrl(String url) {
		 if(url.equals("/")) {
			 url= "";
		 }else if(url.endsWith("/")) {
			 url=url.substring(0,url.length()-1);
		 }
		 return url;
	}

    @Override
    public void afterPropertiesSet() throws Exception {
    	LOCAL_PATH=handlePath(localPath);
    	LOCAL_URL=handleLocalUrl(localUrl);
        ALIOSS_END_POINT = alioss_file_endpoint;
        ALIOSS_ACCESS_KEY_ID = alioss_file_keyid;
        ALIOSS_ACCESS_KEY_SECRET = alioss_file_keysecret;
        ALIOSS_BUCKET_NAME = alioss_file_bucket;
        ALIOSS_FILE_PATH = alioss_file_path;
    }
}
